package com.beijiteshop.shop.ui.detail.adapter

import androidx.databinding.ViewDataBinding
import com.beijiteshop.shop.R
import com.beijiteshop.shop.databinding.HeaderProductTitleBinding
import com.beijiteshop.shop.databinding.ItemProductEvaluationBinding
import com.beijiteshop.shop.model.api.response.EvaluationBean
import com.beijiteshop.shop.model.entity.ProductDetailItemEntity
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/4 14:42
 * @Description 商品详情页面adapter
 */
class ProductDetailAdapter(data: MutableList<ProductDetailItemEntity>)
    : BaseMultiItemQuickAdapter<ProductDetailItemEntity,BaseDataBindingHolder<ViewDataBinding>>(data) {

    init {
        addItemType(ProductDetailItemEntity.TYPE_HEADER_TITLE,R.layout.header_product_title)
        addItemType(ProductDetailItemEntity.TYPE_EVALUATION,R.layout.item_product_evaluation)
    }

    override fun convert(
        holder: BaseDataBindingHolder<ViewDataBinding>,
        item: ProductDetailItemEntity
    ) {
        when(item.itemType){
            ProductDetailItemEntity.TYPE_HEADER_TITLE -> {
                val titleBinding = holder.dataBinding as? HeaderProductTitleBinding
                titleBinding?.let {binding ->
                    if (item.data as? String != null){
                        binding.headerProductTitleTv.text = item.data
                    }
                }
            }
            ProductDetailItemEntity.TYPE_EVALUATION -> {
                val evaluationBinding = holder.dataBinding as? ItemProductEvaluationBinding
                evaluationBinding?.let { binding ->
                    if (item.data as? EvaluationBean != null){
                        binding.evaluation = item.data
                    }
                }
            }
        }
    }
}